namespace Digitalmes.Application.Mes;

/// <summary>
/// 产线信息表服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "mes")]
public sealed class MdLineService : IDynamicWebApiService
{
    private readonly SugarRepository<MdLine> _thisRepository;

    public MdLineService(SugarRepository<MdLine> thisRepository)
    {
        _thisRepository = thisRepository;
    }

    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<MdLineDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .OrderByDescending(m => m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<MdLineDto>>();
    }

    /// <summary>
    /// 查询所以的查询信息，包含对应的工位
    /// </summary>
    /// <returns></returns>
    public async Task<List<MdLineDto>> GetListAsync()
    {
        var query = await _thisRepository.GetListAsync();
        return query.Adapt<List<MdLineDto>>();
    }

    /// <summary>
    /// 根据主键查询
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<MdLineDto> GetAsync(long id)
    {
        var model = await _thisRepository.GetByIdAsync(id);
        return model.Adapt<MdLineDto>();
    }

    /// <summary>
    /// 产线树节点展示
    /// </summary>
    /// <returns></returns>
    [HttpGet]
    public async Task<List<TreeDto>> TreeAsync()
    {
        var lines = await _thisRepository.AsQueryable()
            .Includes(s => s.Stations)
            .ToListAsync();

        List<TreeDto> trees = new(lines.Count);

        foreach (var line in lines)
        {
            TreeDto tree = new()
            {
                Id = line.Id,
                Label = line.LineName,
                Value = line.LineCode,
            };
            trees.Add(tree);

            if (line.Stations != null)
            {
                tree.Children = new List<TreeDto>(line.Stations.Count);
                foreach (var station in line.Stations.OrderBy(s => s.Sort))
                {
                    tree.Children.Add(new()
                    {
                        Id = station.Id,
                        Label = $"{station.OpCode} ({station.OpName})",
                        Value = station.OpCode,
                        IsLeaf = true,
                        ParentId = tree.Id,
                        ParentValue = tree.Value,
                    });
                }
            }
        }

        return trees;
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(MdLineDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.LineCode == model.LineCode))
        {
            Oops.ThrowBah(ErrCode.E4002);
        }

        return await _thisRepository.InsertAsync(model.Adapt<MdLine>());
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(MdLineDto model)
    {
        if (await _thisRepository.IsAnyAsync(s => s.LineCode == model.LineCode && s.Id != model.Id))
        {
            Oops.ThrowBah(ErrCode.E4002);
        }

        return await _thisRepository.UpdateAsync(model.Adapt<MdLine>());
    }

    /// <summary>
    /// 删除,支持多个
    /// </summary>
    /// <param name="ids">逗号分隔</param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody] long id)
    {
        return await _thisRepository.DeleteAsync(m => m.Id == id);
    }
}
